//
//  HMPushOn.m
//  HMDemo
//
//  Created by mengxiangyu on 2017/7/12.
//  Copyright © 2017年 mengxiangyu. All rights reserved.
//

#import "HMPushOn.h"
#import "HMPushOnCell.h"

@interface HMPushOn ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;
@property (assign, nonatomic) CGFloat holdViewHeight; // 保持显示区域高度
@property (assign, nonatomic) CGFloat holdViewTopHeight; // 保持显示区域上侧高度
@property (assign, nonatomic) CGFloat holdViewBottomHeight; // 保持显示区域下侧高度
@property (assign, nonatomic) CGFloat headViewHeight; // 头部最初高度

@property (strong, nonatomic) UIView *headView;
@end

@implementation HMPushOn

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
        
        _tableView = [[UITableView alloc] init];
        [self addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.bounces = NO;
        
        _holdViewBottomHeight = 0;
        _holdViewTopHeight = 0;
        _holdViewHeight = 0;
        _headViewHeight = 0;
    }
    return self;
}

- (void)showHeadview:(UIView *)headView holdViewRect:(CGRect)rect target:(id)target {
    _headView = headView;
    [self addSubview:headView];
    
    self.delegate = target;
    
    _headViewHeight = headView.frame.size.height;
    _holdViewHeight = rect.size.height;
    _holdViewTopHeight = rect.origin.y;
    _holdViewBottomHeight = headView.frame.size.height-CGRectGetMaxY(rect);
    
    _tableView.frame = CGRectMake(0, _holdViewHeight, HM_WIDTH, self.frame.size.height-_holdViewHeight);
    
    UIView *tableHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HM_WIDTH, _headViewHeight-_holdViewHeight)];
    _tableView.tableHeaderView = tableHeadView;
    
    [_tableView reloadData];
}

#pragma mark - tableViewDelegate 及 tableViewDataSource 方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"record";
    HMPushOnCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[HMPushOnCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HM_WIDTH, 34)];
    view.backgroundColor = Color_ControllerView;
    
    UIButton *cellView = [[UIButton alloc] initWithFrame:CGRectMake(0, 2, HM_WIDTH, 30)];
    cellView.backgroundColor = [UIColor whiteColor];
    [view addSubview:cellView];
    
    NSArray *array = @[@{@"color":rgba(255, 160, 188, 1), @"x":@3, @"w":@36, @"title":@"月经期"}, @{@"color":rgba(255, 194, 200, 1), @"x":@62, @"w":@48, @"title":@"预测经期"}, @{@"color":rgba(140, 190, 82, 1), @"x":@133, @"w":@36, @"title":@"安全期"}, @{@"color":rgba(215, 126, 205, 1), @"x":@192, @"w":@36, @"title":@"易孕期"}, @{@"color":rgba(249, 139, 76, 1), @"x":@251, @"w":@36, @"title":@"排卵期"}];
    for (NSInteger i=0; i<array.count; i++) {
        NSDictionary *dict = array[i];
        NSNumber *number = [dict objectForKey:@"x"];
        CGFloat x = [number floatValue];
        UIView *iconView = [[UIView alloc] initWithFrame:CGRectMake(x, 9.5, 12, 12)];
        iconView.backgroundColor = dict[@"color"];
        [cellView addSubview:iconView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(iconView.frame)+3, 5, [[dict objectForKey:@"w"] floatValue], 20)];
        label.font = [UIFont systemFontOfSize:12];
        label.adjustsFontSizeToFitWidth = YES;
        [cellView addSubview:label];
        label.textColor = dict[@"color"];
        label.text = dict[@"title"];
    }
    
    [[cellView rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(id x) {
        XYLog(@"点击了查看详情");
    }];
    
    UIImageView *arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(HM_WIDTH-10-8, 8, 8, 15)];
    arrowImage.image = [UIImage imageNamed:@"more_white"];
    [cellView addSubview:arrowImage];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 2, HM_WIDTH, 0.5)];
    [view addSubview:line];
    line.backgroundColor = Color_ControllerView;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 34;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.y >= _holdViewTopHeight) {
        
        if (_headViewHeight-(scrollView.contentOffset.y-_holdViewTopHeight) > _holdViewHeight + _holdViewTopHeight) {
            _headView.frame = CGRectMake(0, -_holdViewTopHeight, HM_WIDTH, _headViewHeight-(scrollView.contentOffset.y-_holdViewTopHeight));
        } else {
            _headView.frame = CGRectMake(0, -_holdViewTopHeight, HM_WIDTH, _holdViewHeight + _holdViewTopHeight);
        }
        
    } else {
        _headView.frame = CGRectMake(0, -scrollView.contentOffset.y, HM_WIDTH, _headViewHeight);
    }
}


@end
